package com.bonc.modules.rpt.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonc.factory.PageFactory;
import com.bonc.modules.common.param.SysFileInfoParam;
import com.bonc.modules.common.service.SysFileInfoService;
import com.bonc.modules.rpt.dao.FixUploadDao;
import com.bonc.modules.rpt.entity.FixUploadEntity;
import com.bonc.modules.rpt.param.FixParam;
import com.bonc.modules.rpt.service.FixService;
import com.bonc.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author jinlong
 * @date 2021/7/13
 */
@Service("fixUpload")
public class FixUploadServiceImpl extends ServiceImpl<FixUploadDao, FixUploadEntity> implements FixService {

    @Autowired
    private SysFileInfoService sysFileInfoService;


    @Override
    public Page queryPage(FixParam fixParam) {
        QueryWrapper<FixUploadEntity> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(fixParam)) {
            if (ObjectUtil.isNotEmpty(fixParam.getRptType())) {
                queryWrapper.eq("rpt_fix_upload.rpt_type", fixParam.getRptType());
            }
        }
        queryWrapper.eq("rpt_fix_upload.status", 1);
        queryWrapper.orderByDesc("rpt_fix_upload.create_time");
        return this.baseMapper.page(PageFactory.defaultPage(), queryWrapper);
    }

    @Override
    @Transactional
    public Long uploadFile(MultipartFile file, FixParam fixParam) {
        Long fileId = sysFileInfoService.uploadFile(file);
        FixUploadEntity fixUploadEntity = new FixUploadEntity();
        fixUploadEntity.setFileId(fileId);
        fixUploadEntity.setStatus(1);
        fixUploadEntity.setRptType(fixParam.getRptType());
        fixUploadEntity.setCreateTime(DateTime.now());
        fixUploadEntity.setCreateUser(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
        this.save(fixUploadEntity);
        return fileId;
    }

    @Override
    public void downloadFile(FixParam fixParam, HttpServletResponse response) {
        FixUploadEntity fixUploadEntity = this.getById(fixParam.getRptId());
        SysFileInfoParam sysFileInfoParam = new SysFileInfoParam();
        sysFileInfoParam.setId(fixUploadEntity.getFileId());
        sysFileInfoService.download(sysFileInfoParam,response);
    }

    @Override
    @Transactional
    public void deleteFile(FixParam fixParam) {
        FixUploadEntity fixUploadEntity = this.getById(fixParam.getRptId());
        SysFileInfoParam sysFileInfoParam = new SysFileInfoParam();
        sysFileInfoParam.setId(fixUploadEntity.getFileId());
        sysFileInfoService.delete(sysFileInfoParam);
        this.removeById(fixParam.getRptId());
    }
}
